﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using AloneLibrary.Services.Camera;

namespace NetworkStateManagement.Classes
{
    class EnemyHandler : DrawableGameComponent
    {

        Enemy[] Enemies;
        List<Enemy> DrawableEnemies;

        Vector3 position;
        Vector3 direction = Vector3.Zero;
        float scale = 1;
        Random random;
        Vector3 velocity;
        Matrix world;
        Model EnemyModel;
        ThirdPersonCamera camera;
        

        public EnemyHandler(Game game) : base(game) {

            random = new Random();
            velocity = new Vector3((float)random.NextDouble(), 0, (float)random.NextDouble());
            position = new Vector3( (float)random.Next(1000), 0, (float)random.Next(1000));

            Initialize();
            
            DrawableEnemies = new List<Enemy>();

            Enemies = new Enemy[100];
            EnemyModel = game.Content.Load<Model>(@"Models\dude");

            //create some enemies, same objects used forever and ever
            for (int i = 0; i < 100; i++)
            {
                velocity = new Vector3((float)(random.Next(-1000,1000) / 1000.0f), 0, (float)(random.Next(-1000, 1000) / 1000.0f));
                position = new Vector3((float)random.Next(-1000, 1000), 0, (float)random.Next(-1000, 1000));
                Enemies[i] = new Enemy(EnemyModel, position, direction, scale, velocity);
            }
            //plocka ut några som ska ritas... typ

        }

        private void Initialize()
        {
            camera = (ThirdPersonCamera)Game.Services.GetService(typeof(ThirdPersonCamera));
        }

        public override void Update(GameTime gameTime)
        {
            foreach (Enemy enemy in Enemies)
            {
                enemy.Position += enemy.Velocity;
            }

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            foreach (Enemy enemy in Enemies)
            {
                Matrix[] transforms = new Matrix[enemy.Model.Bones.Count];

                enemy.Model.CopyAbsoluteBoneTransformsTo(transforms);

                world = Matrix.CreateScale(enemy.Scale)
                    * Matrix.CreateRotationY(enemy.Direction.Y)
                    * Matrix.CreateTranslation(enemy.Position);

                foreach (ModelMesh modmesh in enemy.Model.Meshes)
                {
                    foreach (BasicEffect effect in modmesh.Effects)
                    {
                        effect.EnableDefaultLighting();
                        effect.PreferPerPixelLighting = true;
                        effect.World = transforms[modmesh.ParentBone.Index] * world;
                        effect.Projection = camera.Projection;
                        effect.View = camera.View;
                    }

                    modmesh.Draw();
                }
            }
            base.Draw(gameTime);
        }

    }
}
